package com.baizhi.ems_thymeleaf.controller;

import com.baizhi.ems_thymeleaf.pojo.Employee;
import com.baizhi.ems_thymeleaf.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    EmpService empService;

    @Value("${file.root.dir}")
    private String realName;


    @RequestMapping("/list")
    public String empList(Model model){
        List<Employee> emplist =  empService.queryAll();
        model.addAttribute("emplist",emplist);
        return "emplist";
    }

    @RequestMapping("/add")
    public String add(Employee employee, MultipartFile img, HttpServletRequest request) throws IOException {
        System.out.println(employee);
        System.out.println(realName);
        System.out.println(img);

        //1.得到原始圖片名稱
        String oriName = img.getOriginalFilename();
        //獲取文件名前綴
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd:hh:mm:ss");
        String filePrefix = simpleDateFormat.format(new Date());
        int index =  oriName.lastIndexOf(".");
        String subfix= oriName.substring(index);
        String newFileName = filePrefix + subfix;
//        String pre = request.getServletContext().getRealPath("/photos");
//        File dir = new File("uploads");
//        if (!dir.exists()){
//            dir.mkdirs();
//        }
        System.out.println(newFileName);

        try {
            img.transferTo(new File(realName, newFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        employee.setPhoto(newFileName);
        empService.save(employee);
        System.out.println(newFileName);
        return "redirect:/list";
    }

    @RequestMapping("/selectByid")
    public String selectByid(Long id,Model model){
        Employee employee =  empService.selectById(id);
        model.addAttribute("emp",employee);
        return "updateEmp";
    }


    @RequestMapping("/update")
    public String update(Employee employee, MultipartFile img, HttpServletRequest request) throws IOException {
        System.out.println(employee);
        System.out.println(realName);
        System.out.println(img);

        //1.得到原始圖片名稱
        String oriName = img.getOriginalFilename();
        //獲取文件名前綴
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd:hh:mm:ss");
        String filePrefix = simpleDateFormat.format(new Date());
        int index =  oriName.lastIndexOf(".");
        String subfix= oriName.substring(index);
        String newFileName = filePrefix + subfix;
//        String pre = request.getServletContext().getRealPath("/photos");
//        File dir = new File("uploads");
//        if (!dir.exists()){
//            dir.mkdirs();
//        }
        System.out.println(newFileName);

        try {
            img.transferTo(new File(realName, newFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        employee.setPhoto(newFileName);
        empService.update(employee);
        System.out.println(newFileName);
        return "redirect:/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        empService.delete(id);
        return "redirect:/list";
    }


}
